using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

public partial class includes_DntRating : System.Web.UI.UserControl, ICallbackEventHandler
{
    #region Define Local Variable
    string _RatingType = "";
    int _dentistId = 0;
    public int FullRate = 0;
    public bool halfRate = false;
    string _imageName = "plus";
    #endregion

    #region Define Properties
    /// <summary>
    /// Name of Rating field In DataBase
    /// </summary>
    public string RatingType
    {
        set { _RatingType = value; }
        get { return _RatingType; }
    }
    /// <summary>
    /// hold Current Dentist ID
    /// </summary>
    public int DentistId
    {
        set { _dentistId = value; }
        get { return _dentistId; }
    }
    public string imageName
    {
        set { _imageName = value; }
        get { return _imageName; }
    }
    decimal _rating = 0.00M;
    public decimal rating
    {
        set { _rating = value; }
        get { return _rating; }
    }
    decimal _totalScore = 5;
    public decimal totalScore
    {
        set { _totalScore = value; }
        get { return _totalScore; }
    }   
    #endregion

    #region Page Load
    protected void Page_Load(object sender, EventArgs e)
    {
        //set Java Script for Client Call back handling
        if (ViewState["DentistId"] == null || ViewState["RatingType"] == null)
        {
            ViewState["DentistId"] = this.DentistId;
            ViewState["RatingType"] = this.RatingType;
        }
        else
        {
            this.DentistId = Convert.ToInt32(ViewState["DentistId"]);
            this.RatingType = ViewState["RatingType"].ToString();
        }
        ClientScriptManager cm = Page.ClientScript;
        String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData" + this.ID, "");
        String callbackScript = "function CallServer" + this.ID + "(arg) {" + cbReference + "; }";
        cm.RegisterClientScriptBlock(this.GetType(), "CallServer" + this.ID + "(arg)", callbackScript, true);

        //read current rating from database  userType                      
        getRating();
    }
    #endregion

    #region Get Rating Method - Read current rating from database userType
    protected void getRating()
    {
        if (this.rating > this.totalScore)
            this.rating = totalScore;
        decimal rate = this.rating / (this.totalScore / 5);
        FullRate = (int)Math.Round(rate, 0);
        //FullRate  =(int)Math.Truncate(rate); //cR;		
        //if ((Math.Round((Math.Round(rate, 1) - FullRate), 1) >= .3M && (Math.Round((Math.Round(rate, 1) - FullRate), 1)) <= .7M))
        //    halfRate = true; 
        //if ((Math.Round((Math.Round(rate, 1) - FullRate), 1)) > .7M)
        //FullRate += 1;        
    }
    #endregion

    #region ICallbackEventHandler Members
    
    string userLastRating = "0";

    #region GetCallBackResult Method
    public string GetCallbackResult()
    {
        return userLastRating; //getRating().ToString();
    }
    #endregion

    #region RaiseCallbackEvent Method - Handle callback insert new rating in database
    public void RaiseCallbackEvent(string eventArgument)
    {   ///handle callback insert new rating in database
        decimal rateByUser = Convert.ToDecimal(eventArgument) * (this.totalScore / 5);
        userLastRating = rateByUser.ToString();
        // Database  code
        try
        {
            #region -- old code --
            //SqlParameter[] sqlParam = new SqlParameter[4];
            //sqlParam[0] = new SqlParameter("@Dnt_Id", SqlDbType.Int);
            //sqlParam[0].Value = DentistId;

            //sqlParam[1] = new SqlParameter("@usr_id", SqlDbType.Int);
            //sqlParam[1].Value = Convert.ToInt32(Session["userID"].ToString());

            //sqlParam[2] = new SqlParameter("@RatingType", SqlDbType.VarChar, 100);
            //sqlParam[2].Value = this.RatingType;
            ////  sqlParam[2] = new SqlParameter("@rateupd", SqlDbType.Bit);
            
            //sqlParam[3] = new SqlParameter("@ratevalue", SqlDbType.Decimal);
            //sqlParam[3].Precision = 18;
            //sqlParam[3].Scale = 2;
            //sqlParam[3].Value = rateByUser;

            ////sqlParam[4] = new SqlParameter("@RaterType", SqlDbType.VarChar,10);
            ////sqlParam[4].Value = "doctor"; // We need to change. This is just for testing purpose

            //Manager mgr = new Manager(true,"ConnectionString");
            ////userLastRating = "insert_patientreview " + sqlParam[0].Value + "," + sqlParam[1].Value + "," + sqlParam[2].Value + "," + sqlParam[3].Value + "," + sqlParam[4].Value;
            //mgr.ExecuteUpdate("insert_patientDentistreview", sqlParam);
            #endregion

            SqlParameter[] ParamInsert = new SqlParameter[5];
            ParamInsert[0] = new SqlParameter("@DntId", SqlDbType.Int);
            ParamInsert[0].Value = DentistId;

            ParamInsert[1] = new SqlParameter("@UsrId", SqlDbType.Int);
            ParamInsert[1].Value = Convert.ToInt32(Session["userID"].ToString());

            ParamInsert[2] = new SqlParameter("@RatingType", SqlDbType.VarChar, 100);
            ParamInsert[2].Value = this.RatingType;

            ParamInsert[3] = new SqlParameter("@RateValue", SqlDbType.Decimal);
            ParamInsert[3].Precision = 18;
            ParamInsert[3].Scale = 2;
            ParamInsert[3].Value = rateByUser;

            ParamInsert[4] = new SqlParameter("@IsDentist", SqlDbType.Bit);
            ParamInsert[4].Value = false;

            Manager mgr = new Manager(true,"ConnectionString"); 
            mgr.ExecuteUpdate("InsertDentistReviews", ParamInsert);
            //mgr.ExecuteUpdate("insert into tmp (txt) values ('" + this.RatingType + "')");
            mgr = null;
        }
        catch (Exception ex)
        {
            userLastRating = ex.ToString();            
        }
    }
    #endregion

    #endregion
}
